﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DevExpress.MailClient.Win.Controls
{
    public partial class ucArchiveFileViewer : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY;
        [Browsable(false)]
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY
        {
            get { return _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY; }
            set
            {
                _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = value;
                //1. LOAD FILE PHYSICAL VIEWER
                LOAD_FILE_PHYSICAL_VIEWER();
                //LOAD FILE DYNAMIC INDECIES VALUES
                LOAD_FILE_DYNAMIC_INDECIES_VALUES();

            }
        }

        private void LOAD_FILE_DYNAMIC_INDECIES_VALUES()
        {
            ucFileDynamicIndeciesDetails1.CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY;
        }

        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE aRCHIVE_DEPARTMENT_FILE;
        [Browsable(false)]
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE
        {
            get { return aRCHIVE_DEPARTMENT_FILE; }
            set
            {
                aRCHIVE_DEPARTMENT_FILE = value;
                if (!aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty))
                {
                    //2. LOAD FILE BASIC INFORMATION
                    LOAD_FILE_BASIC_INFORMATION();
                    //3. LOAD FILE DYNAMIC INDECIES STRUCTURE
                    LOAD_FILE_DYNAMIC_INDECIES_STRUCTURE();
                    //4. LOAD FILE CHANGES HISTORY
                    LOAD_FILE_CHANGES_HISTORY();
                }
            }
        }

        private void LOAD_FILE_CHANGES_HISTORY()
        {
            ucFileHistories1.CURRENT_ARCHIVE_DEPARTMENT_FILE = CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE;
        }

        private void LOAD_FILE_DYNAMIC_INDECIES_STRUCTURE()
        {
            ucFileDynamicIndeciesDetails1.CURRENT_ARCHIVE_DEPARTMENT_FILE = CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE;
        }

        private void LOAD_FILE_BASIC_INFORMATION()
        {
            ucFileBasicInformations1.CURRENT_ARCHIVE_DEPARTMENT_FILE = CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE;
        }

        internal bool PHYSICAL_FILE_FOUND
        { get; set; }

        private bool LOAD_FILE_PHYSICAL_VIEWER()
        {
            ucFilePhysicalViewer1.CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY;
            return PHYSICAL_FILE_FOUND = ucFilePhysicalViewer1.PHYSICAL_FILE_FOUND;
        }

        public ucArchiveFileViewer()
        {
            InitializeComponent();

            CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE();
        }

        internal PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY SAVE_SELECTED_FILE_DYNAMIC_INDICIES()
        {
            PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY sAVED_ARCHIVE_DEPARTMENT_FILE_HISTORY = ucFileDynamicIndeciesDetails1.SAVE_SELECTED_FILE_DYNAMIC_INDICIES();
            if (sAVED_ARCHIVE_DEPARTMENT_FILE_HISTORY != null)
            {

                //1. refresh the file history
                ucFileHistories1.CURRENT_ARCHIVE_DEPARTMENT_FILE = (from files in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILEs
                                                                    where files.ARCHIVE_DEPARTMENT_FILE_ID.Equals(CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID)
                                                                    select files).FirstOrDefault();
            }
            return sAVED_ARCHIVE_DEPARTMENT_FILE_HISTORY;
        }

        internal void DOWNLOAD_SELECTED_FILE()
        {
            ucFilePhysicalViewer1.DOWNLOAD_SELECTED_FILE();
        }

        internal void LOCK_CURRENT_FILE()
        {
            ucFileBasicInformations1.LOCK_CURRENT_FILE();
        }

        internal void UNLOCK_CURRENT_FILE()
        {
            ucFileBasicInformations1.UNLOCK_CURRENT_FILE();
        }

        //internal PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE SAVE_SELECTED_FILE_BASIC_DATA(Guid guid)
        //{
        //    return ucFileBasicInformations1.SAVE_SELECTED_FILE_BASIC_DATA(guid);
        //}

        internal void SAVE_SELECTED_FILE_BASIC_DATA()
        {
            ucFileBasicInformations1.SAVE_ARCHIVE_DEPARTMENT_FILES_BASIC_DATA(ucFileDynamicIndeciesDetails1.ARCHIVE_DOCUMENT_TYPE);

            PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILES_BASIC_HISTORYInsert
                (
                    ucFileDynamicIndeciesDetails1.CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID,
                    ucFileBasicInformations1.CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID.Value,
                    ucFileBasicInformations1.CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME,
                    ucFileDynamicIndeciesDetails1.ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID,
                    ucFileBasicInformations1.CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_GENERATED_BARCODE_VALUE,
                    ucFileBasicInformations1.CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NOTES,
                    ucFileBasicInformations1.CURRENT_ARCHIVE_DEPARTMENT_FILE.IS_DELETED.Value
                );
        }

        internal void PRINT_FILE_BARCODE()
        {
            ucFileBasicInformations1.PRINT_BARCODE();
        }
    }
}
